﻿using System;
using System.Linq;
using System.Web.UI;
using AutoStopApp.Core;
using AutoStopData.BussinessLogic;
using AutoStopData.Domains;
using AutoStopData.Model;

namespace AutoStopApp
{
    public partial class Site : MasterPage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += PageLoad;
            lbLogOut.Click += lbLogOutClick;
            lbChangePassword.Click += lbChangePasswordClick;
            SessionProvider.Initialize(this);
            ucLogIn.InitializeAjaxControls(ramMain, rlpMain);

            var ajaxPage = Page as AjaxPage;
            if (ajaxPage != null)
            {
                ajaxPage.InitializeAjaxControls(ramMain, rlpMain);
            }
        }

        void PageLoad(object sender, EventArgs e)
        {
            if (SessionProvider.CanLogActivity())
            {
                Sessions.UpdateLastAccess(SessionProvider.CurrentSession.SessionId);
            }

            ltrDate.Text = DateTime.Now.ToShortDateString();
            var session = SessionProvider.CurrentSession;
            var userTypeName = GetUserTypeName(session);
            if (session != null)
            {
                ltrUsername.Text = session.User.Name;
                linkToRegistration.Visible = false;
                pnlLogIn.Visible = false;
                lbLogOut.Visible = true;
                lbChangePassword.Visible = true;
            }
            else
            {
                ltrUsername.Text = "anonym";
                linkToRegistration.Visible = true;
                pnlLogIn.Visible = true;
                lbLogOut.Visible = false;
                lbChangePassword.Visible = false;
            }
            if (!IsPostBack)
            {
                GenerateMenu(userTypeName);
            }
            var accessItem = WsaaManager.Data.FirstOrDefault(i => i.UserTypeName == userTypeName && Request.Url.AbsoluteUri.ToUpper().Contains(i.MenuItem.Url.ToUpper()));
            if (accessItem == null)
            {
                Response.Redirect("/Default.aspx?message=NOT_AUTHORIZED");
            }
        }

        static void lbLogOutClick(object sender, EventArgs e)
        {
            SessionProvider.LogOut();
        }

        private void lbChangePasswordClick(object sender, EventArgs e)
        {
            Response.Redirect(WsaaManager.Data.First(ai => ai.Resource == WsaaManager.WsaaResource.PasswordChangePage && ai.UserTypeName == GetUserTypeName(SessionProvider.CurrentSession)).MenuItem.Url);
        }

        private static UserTypes.Name GetUserTypeName(Session session)
        {
            var retVal = UserTypes.Name.None;
            if (session != null)
            {
                retVal = UserTypes.GetName(session.User.UserTypeCode);
            }
            return retVal;
        }

        private void GenerateMenu(UserTypes.Name userTypeName)
        {
            var accessItems = WsaaManager.Data.Where(i => i.UserTypeName == userTypeName && !String.IsNullOrEmpty(i.MenuItem.Text));
            rptMenu.DataSource = accessItems;
            rptMenu.DataBind();
        }
    }
}